package bean.control;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;

import util.UploadedImage;
import util.Util;
import amazon.s3.S3;
import bean.entity.ProductBean;
import bean.entity.UserBean;
import bean.util.ProductFileUpload;
import bean.view.View;
import eao.EaoCity;
import eao.EaoCountry;
import eao.EaoLocation;
import eao.EaoProduct;
import eao.EaoProductPicture;
import entity.City;
import entity.Country;
import entity.Product;
import entity.ProductPicture;
import entity.User;

@ManagedBean(name="productc")
@ViewScoped
public class ProductControl implements Serializable {
	private static final long serialVersionUID = 139077269494165440L;
	
	@EJB EaoLocation eaoLocation;
	@EJB EaoCountry eaoCountry;
	@EJB EaoCity eaoCity;
	@EJB EaoProduct eaoProduct;
	@EJB EaoProductPicture eaoProductPicture;

	@ManagedProperty("#{userc.userb}")
	private UserBean userb;
	
	@ManagedProperty("#{productBean}")
	private ProductBean productb;
	
	private Product product;
	
	private User user;
	
	private List<Product> products;
	
	private View view;
	
	private boolean edit;
	
	@PostConstruct
	public void init(){
		view = new View();
	}
	
	public void add(){
		try {

			this.checkLocation();
			
			// assoaciate the user with the product
			productb.getProduct().setUser(userb.getUser());
			// saving the new product in the db
			eaoProduct.create(productb.getProduct());
			
			for (String picture : productb.getPictures()){
				eaoProductPicture.create(new ProductPicture(picture, productb.getProduct()));
				productb.getProduct().getProductPictures().add(new ProductPicture(picture, productb.getProduct()));
			}
			productb.getPictures().clear();
			
			// add the new product to the user's product list
			userb.getUser().getProducts().add(productb.getProduct());
			
			view.successMessage("Produto cadastrado com sucesso!");
			
		} catch (Exception e) {
			view.errorMessage("ocorreu um erro ao cadastrar seu produto, por favor tente novamente");
			e.printStackTrace();
		}
		
	}

	private void checkLocation(){
		// first save the location
		City city = eaoCity.findByName(productb.getProduct().getLocation().getCity().getCity());
		if (city == null){
			city = new City();
			city.setCity(productb.getProduct().getLocation().getCity().getCity());
			eaoCity.create(city);
		}
		productb.getProduct().getLocation().setCity(city);
		productb.getProduct().getLocation().setUser(userb.getUser());
		productb.getProduct().getLocation().setCountry(new Country(new Integer(30), "BRAZIL"));
		
		try {
			eaoLocation.create(productb.getProduct().getLocation());
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public void edit(Product p){
		view = new View();
		productb.setProduct(p);
		edit = true;
	}
	
	public void delete(Product p){
		view = new View();
		
		eaoProduct.delete(p);
		userb.getUser().getProducts().remove(p);
		edit = false;
		view.successMessage("Produto excluído com sucesso!");
	}
	
	public void update(){
		eaoProduct.update(this.productb.getProduct());
		
		view = new View();
		view.successMessage("Produto atualizado com sucesso!");

	}
	
	public void cancel(){
		this.product = new Product();
		edit = false;
		
	}
	
	public UserBean getUserb() {
		return userb;
	}

	public void setUserb(UserBean userb) {
		this.userb = userb;
	}

	public ProductBean getProductb() {
		return productb;
	}

	public void setProductb(ProductBean productb) {
		this.productb = productb;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public boolean isEdit() {
		return edit;
	}

	public void setEdit(boolean edit) {
		this.edit = edit;
	}

	public List<Product> getProducts() {
		return products;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	
	
}
